netlify_lambda_http 0.2.0

Application Load Balancer and API Gateway event types for AWS Lambda
Documentation

Enriches the lambda crate with http types targeting AWS ALB, API Gateway REST and HTTP API lambda integrations.

This crate abstracts over all of these trigger events using standard http types minimizing the mental overhead of understanding the nuances and variation between trigger details allowing you to focus more on your application while also giving you to the maximum flexibility to transparently use whichever lambda trigger suits your application and cost optimiztions best.

Examples

Hello World

lambda_http handlers adapt to the standard lambda::Handler interface using the handler function.

The simplest case of an http handler is a function of an http::Request to a type that can be lifted into an http::Response. You can learn more about these types here.

Adding an #[lambda(http)] attribute to a #[tokio::run]-decorated main function will setup and run the Lambda function.

Note: this comes at the expense of any onetime initialization your lambda task might find value in. The full body of your main function will be executed on every invocation of your lambda task.

use netlify_lambda_http::{
lambda::{lambda, Context},
IntoResponse, Request,
};
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[lambda(http)]
#[tokio::main]
async fn main(_: Request, _: Context) -> Result<impl IntoResponse, Error> {
Ok("👋 world!")
}

Hello World, Without Macros

For cases where your lambda might benfit from one time function initializiation might prefer a plain main function and invoke netlify_lambda::run explicitly in combination with the handler function. Depending on the runtime cost of your dependency bootstrapping, this can reduce the overall latency of your functions execution path.

use netlify_lambda_http::{handler, lambda};

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[tokio::main]
async fn main() -> Result<(), Error> {
// initialize dependencies once here for the lifetime of your
// lambda task
netlify_lambda::run(handler(|request, context| async { Ok("👋 world!") })).await?;
Ok(())
}

Leveraging trigger provided data

You can also access information provided directly from the underlying trigger events, like query string parameters, with the RequestExt trait.

use netlify_lambda_http::{handler, lambda::{self, Context}, IntoResponse, Request, RequestExt};

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[tokio::main]
async fn main() -> Result<(), Error> {
netlify_lambda::run(handler(hello)).await?;
Ok(())
}

async fn hello(
request: Request,
_: Context
) -> Result<impl IntoResponse, Error> {
Ok(format!(
"hello {}",
request
.query_string_parameters()
.get("name")
.unwrap_or_else(|| "stranger")
))
}